package factory;

import eduni.simanim.Anim_applet;
import eduni.simanim.Anim_port;
import eduni.simjava.Sim_system;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

/**
 *
 * @author eagle
 */
// The main applet
public class AppletFactory extends Anim_applet implements ItemListener {

    JPanel inputs;
    JLabel msgLabel, sinkLabel, srcLabel, numMachineLabel, showMsgLabel;
    JTextField jobsField, delayMachineField, delayJobsField;
    Choice numMachinesChoice;
    Checkbox showMsgBox;
    static public boolean show_msg = true;

    // initialise the gui inputs
    @Override
    public void anim_init() {
        int i;
        inputs = new JPanel();
        inputs.setLayout(new GridLayout(0, 2));

        msgLabel = new JLabel("Numero de trabajos:", Label.RIGHT);
        inputs.add(msgLabel);
        jobsField = new JTextField("5", 3);
        inputs.add(jobsField);

        numMachineLabel = new JLabel("Numero de maquinas:", Label.RIGHT);
        inputs.add(numMachineLabel);
        numMachinesChoice = new Choice();
        for (i = 1; i <= 10; i++) {
            numMachinesChoice.addItem(" " + i + " ");
        }
        numMachinesChoice.select(2);
        numMachinesChoice.addItemListener(this);
        inputs.add(numMachinesChoice);

        showMsgLabel = new JLabel("Ver Mensajes:", Label.RIGHT);
        inputs.add(showMsgLabel);
        showMsgBox = new Checkbox("");
        showMsgBox.setState(true);
        inputs.add(showMsgBox);

        srcLabel = new JLabel("Delay entre trabajos enviados por el Sender ~e (media):", Label.RIGHT);
        inputs.add(srcLabel);
        delayJobsField = new JTextField("0.4", 5);
        inputs.add(delayJobsField);

        sinkLabel = new JLabel("Delay entre procesamiento de las maquinas ~N (media):", Label.RIGHT);
        inputs.add(sinkLabel);
        delayMachineField = new JTextField("1.0", 5);
        inputs.add(delayMachineField);

        this.add("North", inputs);
    }

    // Setup de la annimacion
    @Override
    public void anim_layout() {
        int num_jobs, i, num_machines;
        double jobs_hold, machine_hold;

//        System.out.println("Laying out simulation");
        num_jobs = Integer.parseInt(jobsField.getText());
        num_machines = numMachinesChoice.getSelectedIndex() + 1;
        jobs_hold = Double.valueOf(delayJobsField.getText()).doubleValue();
        //demora de las maquinas 
        machine_hold = Double.valueOf(delayMachineField.getText()).doubleValue();
        show_msg = showMsgBox.getState();

        // Agregamos las entidades
        Sim_system.add(new Sender("Sender", num_jobs, jobs_hold, 40, 20));
        Sim_system.add(new Bucket("Bucket_1", 110 + 70 * num_machines, 20, Anim_port.LEFT));
        Sim_system.add(new Bucket("Bucket_2", 40, 160, Anim_port.RIGHT));


        for (i = 1; i <= num_machines; i++) {
            Sim_system.add(new Mrouter("Mrouter_" + i, 40 + 70 * i, 20));
            Sim_system.add(new Machine("Machine_" + i, machine_hold, 40 + 70 * i, 90));
            Sim_system.add(new Rrouter("Rrouter_" + i, 40 + 70 * i, 160));
        }

        /**
         * Vinculamos los puertos de ambas entidades para "programar" los
         * eventos
         *
         * @param ent1 Nombre de la primera entidad
         * @param port1 Nombre del puerto de la primera entidad
         * @param ent2 Nombre de la segunda entidad
         * @param port2 Nombre del puerto de la segunda entidad
         */
        Sim_system.link_ports("Sender", "first", "Mrouter_1", "in");
        Sim_system.link_ports("Mrouter_" + num_machines, "next", "Bucket_1", "in");
        Sim_system.link_ports("Rrouter_1", "out", "Bucket_2", "in");

        for (i = 1; i <= num_machines; i++) {
            if (i != num_machines) {
                Sim_system.link_ports("Mrouter_" + i, "next", "Mrouter_" + (i + 1), "in");
            }

            Sim_system.link_ports("Mrouter_" + i, "machine", "Machine_" + i, "in");
            Sim_system.link_ports("Machine_" + i, "out", "Rrouter_" + i, "in1");
            if (i != 1) {
                Sim_system.link_ports("Rrouter_" + i, "out", "Rrouter_" + (i - 1), "in2");
            }
        }
    }

    // Redimensinamos el canvas si cambia el numero de maquinas
    @Override
    public void itemStateChanged(ItemEvent e) {
        Object source = e.getSource();
        if (source == numMachinesChoice) {
            anim_layout(); // re
        }
    }

    // Choose animation output detail
    @Override
    public void anim_output() {
        generate_report(true);
        generate_messages(true);
        Sim_system.set_report_detail(true, false);
    }
    //public boolean handleEvent(Event e) {
    //  if (e.id == Event.ACTION_EVENT)
    //    if (e.target == numWorkChoice)
    //      anim_relayout();
    //  return super.handleEvent(e); // VERY IMPORTANT!!!!
    //}
    
            
    
   
    
}